package 剑指offer;

public class Offer53_JZ37_3 {
    public static int searchIndex(int[] nums) {
        for (int i=0; i<nums.length; i++) {
            if (nums[i] == i) {
                return i;
            }
        }
        return -1;
    }

    public static int searchIndex2(int[] nums) {
        int left = 0, right = nums.length-1;
        while (left <= right) {
            int mid = (left+right) / 2;
            if (nums[mid] == mid) {
                return mid;
            } else if (nums[mid] < mid) {
                left = mid+1;
            } else {
                right = mid-1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {-3, -1, 1, 3, 5};
        System.out.println(searchIndex(arr));
        System.out.println(searchIndex2(arr));
    }
}
